package com.digua.atools.tools;

import com.alibaba.fastjson.JSON;
import com.digua.atools.bean.KV;
import com.digua.atools.tools.listener.DataToDataListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:digua
 * @dat:2021/8/21
 * @description:Map工具类
 */
public class MapUtil {

    /**
     * 获取map的key值
     *
     * @param map
     * @return
     */
    public static List getKeys(Map map) {
        if (map == null) {
            return null;
        }
        if (map.size() == 0) {
            return new ArrayList<>();
        }
        return new ArrayList(map.keySet());
    }

    /**
     * 获取数据值
     *
     * @param map
     * @param key
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> V getVal(Map<K, V> map, K key) {
        if (key == null || map == null) {
            return null;
        }
        return getVal(map, key, null);
    }

    /**
     * 获取数据值
     *
     * @param map
     * @param key
     * @param def 默认值
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> V getVal(Map<K, V> map, K key, V def) {
        if (key == null || map == null) {
            return def;
        }
        return map.keySet().contains(key) ? map.get(key) : def;
    }

    /**
     * List TO Map V值为List元素类型时
     *
     * @param datas
     * @param kListener
     * @param <T>
     * @param <K>
     * @return
     */
    public static <T, K> Map<K, T> toMap(List<T> datas, DataToDataListener<T, K> kListener) {
        if (datas == null || kListener == null) {
            return null;
        }
        Map<K, T> res = new HashMap();
        if (datas.size() == 0) {
            return res;
        }
        for (T data : datas) {
            res.put(kListener.d2d(data), data);
        }
        return res;
    }

    public static <K, V> Map<K, V> toMap(KV<K, V>... kvs) {
        Map<K, V> map = new HashMap<>();
        if (ArrUtil.isBlank(kvs)) {
            return map;
        }
        for (KV kv : kvs) {
            map.put((K) kv.getK(), (V) kv.getV());
        }
        return map;
    }

    /**
     * List TO Map
     *
     * @param datas
     * @param kListener
     * @param <T>
     * @param <K>
     * @return
     */
    public static <T, K, V> Map<K, V> toMap(List<T> datas, DataToDataListener<T, K> kListener, DataToDataListener<T, V> vListener) {
        if (datas == null || kListener == null || vListener == null) {
            return null;
        }
        Map<K, V> res = new HashMap();
        if (datas.size() == 0) {
            return res;
        }
        for (T data : datas) {
            res.put(kListener.d2d(data), vListener.d2d(data));
        }
        return res;
    }

    /**
     * 判断是否为空的map
     *
     * @param map
     * @return
     */
    public static boolean isBlank(Map map) {
        return map == null || map.size() <= 0;
    }

    /**
     * 判断是否不为空的map
     *
     * @param map
     * @return
     */
    public static boolean isNotBlank(Map map) {
        return !isBlank(map);
    }

    /**
     * 转成get请求的参数结构
     *
     * @param paramMap
     * @return
     */
    public static <T> String toGetReqParam(Map<String, T> paramMap) {
        if (isBlank(paramMap)) {
            return "";
        }
        String res = "";
        for (Map.Entry<String, T> entry : paramMap.entrySet()) {
            T val = entry.getValue();
            if (val != null && !com.digua.atools.tools.ObjUtil.isBaseDataOrStr(val)) {
                res += entry.getKey() + "=" + JSON.toJSONString(entry.getValue()) + "&";
            } else {
                res += entry.getKey() + "=" + entry.getValue().toString() + "&";
            }
        }
        return res.length() < 1 ? res : res.substring(0, res.length() - 1);
    }
}
